package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.Painter;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompConector._arrowType;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;

public class CompActor extends Componente {

	final static int MaxCharacterPerLine = 10;
	final static int Margin = 15;
	final static int Head_Radius = 20;
	final static int Arm_Long = 27;
	// Altura Total de actor 7 veces el radio de la cabeza

	private ArrayList<String> TextLine = new ArrayList<String>();
	private String longestString = "Caso";
	private Point textPoint;
	private int textLineSpan;

	public CompActor() {
		_centerPoint = new Point(0, 0);
		textPoint = new Point(0, 0);
		textLineSpan = 0;

		_connectionPoints = new Point[2];
	}

	@Override
	public void setTitulo(String titulo) {
		Titulo = titulo;
		TextLine = DividirTitulo(titulo);
		int aux;
		for (String value : TextLine) {
			aux = value.length();
			if (aux > longestString.length()) {
				longestString = value;
			}
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		int posX = _centerPoint.x + offSetX;
		int posY = _centerPoint.y + offSetY;
		int textWidht;
		Paint DummyPaint = Painter.getInstancia().getBorderPaint(_currentState); // Paint
																					// utilizado
																					// para
																					// el
																					// actor

		// Dibujamos el Actor
		c.drawCircle((Anchor.x + (_width / 2) + offSetX) * globalScale,
				(Anchor.y + Head_Radius + offSetY) * globalScale, Head_Radius
						* globalScale, DummyPaint); // Cabeza
		c.drawLine((Anchor.x + (_width / 2) + offSetX) * globalScale, (Anchor.y
				+ (Head_Radius * 2) + offSetY)
				* globalScale, (Anchor.x + (_width / 2) + offSetX)
				* globalScale, (Anchor.y + (Head_Radius * 4.5f) + offSetY)
				* globalScale, DummyPaint); // Torzo
		c.drawLine((Anchor.x + (_width / 2) + offSetX - Arm_Long - 2)
				* globalScale, (Anchor.y + (Head_Radius * 2.7f) + offSetY)
				* globalScale, (Anchor.x + (_width / 2) + offSetX + Arm_Long)
				* globalScale, (Anchor.y + (Head_Radius * 2.7f) + offSetY)
				* globalScale, DummyPaint); // Manos
		c.drawLine((Anchor.x + (_width / 2) + offSetX) * globalScale, (Anchor.y
				+ (Head_Radius * 4.5f) + offSetY)
				* globalScale, (Anchor.x + (_width / 2) + offSetX - Arm_Long)
				* globalScale, (Anchor.y + (Head_Radius * 6.5f) + offSetY)
				* globalScale, DummyPaint); // Pierna Izq
		c.drawLine((Anchor.x + (_width / 2) + offSetX) * globalScale, (Anchor.y
				+ (Head_Radius * 4.5f) + offSetY)
				* globalScale, (Anchor.x + (_width / 2) + offSetX + Arm_Long)
				* globalScale, (Anchor.y + (Head_Radius * 6.5f) + offSetY)
				* globalScale, DummyPaint);// Pierna Der

		// Dibujamos el texto
		for (int i = 0; i < TextLine.size(); i++) {
			textWidht = (int) Painter.getInstancia()
					.getTextPaint(false, globalScale)
					.measureText(TextLine.get(i));
			c.drawText(TextLine.get(i), ((posX * globalScale) - textWidht / 2),
					((textPoint.y + offSetY) + (i * textLineSpan))
							* globalScale,
					Painter.getInstancia().getTextPaint(false, globalScale));
		}

		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}

	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if ((touch.x <= Anchor.x + _width) && (touch.x >= Anchor.x)) {
			if ((touch.y <= Anchor.y + _height) && (touch.y >= Anchor.y)) {
				respuesta = true;
			}
		}
		return respuesta;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		_connectionPoints[0] = new Point(_centerPoint.x + Arm_Long,
				(int) (Anchor.y + Head_Radius * 2.7));
		_connectionPoints[1] = new Point(_centerPoint.x - Arm_Long,
				(int) (Anchor.y + Head_Radius * 2.7));
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsBegining) {
				value.CalcularConexiones(this, VistaDiagrama.getVista()
						.getDiagrama().getComponenteById(value._childId));
			}
		}
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsEnd) {
				value.CalcularConexiones(VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(value._fatherId), this);
			}
		}
	}

	@Override
	public void actualizarPuntoCentral() {
		Rect mBounds = new Rect();
		int TotalTextHeight = 0;
		for (int i = 0; i < TextLine.size(); i++) {
			Painter.getInstancia().getTextPaint(false, 1)
					.getTextBounds(TextLine.get(i), 0, 2, mBounds);
			TotalTextHeight += (int) ((float) mBounds.height() * 1.2);
		}
		_width = Painter.getInstancia().getTextPaint(false, 1)
				.measureText(longestString);
		if (_width < (Arm_Long * 2) + 2) {
			_width = (Arm_Long * 2) + 2;
		}
		_height = TotalTextHeight + (10.5f * Head_Radius + Margin);
		_centerPoint.x = (int) (Anchor.x + (_width / 2));
		_centerPoint.y = (int) (Anchor.y + (_height / 2));
		textPoint.x = (int) (_centerPoint.x - (Painter.getInstancia()
				.getTextPaint(false, 1).measureText(longestString) / 2));
		textPoint.y = (int) (Anchor.y + (Head_Radius * 7f) + Margin);
		if (TextLine.size() > 0) {
			textLineSpan = TotalTextHeight / TextLine.size();
		}
	}

	@Override
	public String generarDOM() {
		String DOM = "\t<Componente Tipo=\"Actor\" Titulo=\"" + Titulo
				+ "\" ID=\"" + ID + "\" AnchorX=\"" + Anchor.x
				+ "\" AnchorY=\"" + Anchor.y + "\">\n";
		DOM = DOM + "\t</Componente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		// TODO Auto-generated method stub

	}

	@Override
	public Point requestConnectionPoint(CompConector connector, int mode,
			Point otherCompCenter) {
		float distMin = Float.MAX_VALUE;
		float dX = 0;
		float dY = 0;
		float disActual;
		int CP_MasCercano = 0; // Punto de conexion con menor distancia
		for (int i = 0; i < _connectionPoints.length; i++) {
			dX = _connectionPoints[i].x - otherCompCenter.x;
			dY = _connectionPoints[i].y - otherCompCenter.y;
			disActual = (float) Math.sqrt(dX * dX + dY * dY);
			if (disActual < distMin) {
				distMin = disActual;
				CP_MasCercano = i;
			}
		}
		return _connectionPoints[CP_MasCercano];

	}

	public ArrayList<String> DividirTitulo(String titulo) {
		ArrayList<String> respuesta = new ArrayList<String>();
		String[] arreglo = titulo.split(" ");
		String linea = "";
		int acumulatedChar = 0;
		for (int i = 0; i < arreglo.length; i++) {
			acumulatedChar += arreglo[i].length();
			linea += arreglo[i] + " ";
			if (acumulatedChar >= MaxCharacterPerLine) {
				respuesta.add(linea);
				linea = "";
				acumulatedChar = 0;
			}
		}
		if (acumulatedChar > 0) {
			respuesta.add(linea);
		}
		return respuesta;
	}

	@Override
	public void mover(int distX, int distY) {
		Anchor.x += distX;
		Anchor.y += distY;
		actualizarPuntoCentral();
		actualizarAchorsPoints();

	}

	@Override
	public String getTipo() {
		return "Actor";
	}
}
